import { useUser } from './../stores/userStore';
import { api } from '../boot/axios';
import { searchTaskType } from 'src/services/type'
// import { Dialog } from 'quasar'
// import { useRouter } from 'vue-router';

// const router = useRouter()

export function auth(username: string, password: string) {
  return api({
    url: 'auth-token/',
    method: 'post',
    data: {
      username,
      password,
    },
  }).catch(err=>{
    if(err.response.status == 403){
      const user = useUser()
      user.logout()
    }
  });
}


export function getMe(){
  return api({
    url: '/api/users/me/',
    method: 'get',
  })
}

export function getUserAdminUrl(){
  return api({
    url: '/api/users/getUserAdminUrl/',
    method: 'post',
  })
}

export function getAdminUrl(){
  return api({
    url: '/api/users/getAdminUrl/',
    method: 'post',
  })
}

export function fetchChangePassword(old_password: string, new_password: string, confirm_new_password:string){
  return api({
    url: '/api/users/changepassword/',
    method: 'post',
    data:{
      old_password, 
      new_password, 
      confirm_new_password
    }
  })
}

/**
 * 查询用户的姓名、邮箱地址
 * @param data 
 * @returns 
 */
export function selectUserEmail(data:searchTaskType) {
  const { username='', email='', page_size='10', page_num='1' } = data || {};
  return api({
    url: `/api/users/search_user_email/?page_size=${page_size}&page_num=${page_num}`,
    method: 'post',
    data:{
      username,
      email, 
    }
  })
}